<template>
    <view>
        <block v-if="isAdmin">
            <cmpt-comm-list
                type="admin-news"
                :search="search"
                :_menus="sortMenus"
                :_items="sortItems"
                route="admin/news_list"
                sortMenusDefaultIndex="0"
                topBottom="50"
                placeholder="搜索标题"
                @list="bindCommListCmpt"
            >
                <view slot="searchEnd">
                    <button @tap="url" data-url="../add/admin_news_add" class="btn radius bg-admin text-white margin-right-s">
                        <text class="icon-roundadd margin-right-xxs"></text>
                        创建文章
                    </button>
                </view>
                <!-- List Begin -->
                <view class="admin-comm-list">
                    <view v-if="dataList && dataList.total" class="load text-grey">共有{{ dataList.total }}条符合条件记录</view>

                    <view class="item" v-for="(item, index) in dataList.list" :key="index">
                        <view class="header">
                            <view class="left text-cut">
                                <text v-if="item.NEWS_HOME == 0" class="text-black margin-right-xxs" style="font-weight: normal; font-size: 24rpx">[首页推荐]</text>
                                {{ item.NEWS_TITLE }}
                            </view>
                            <view class="right">
                                <text v-if="item.NEWS_STATUS == 1" class="text-black">正常</text>
                                <text v-else-if="item.NEWS_STATUS == 0" class="text-orange">停用</text>
                            </view>
                        </view>

                        <view class="info">
                            <view class="info-item" @tap="url" data-type="phone" :data-url="item.USER_MOBILE">
                                <view class="title">类型</view>
                                <view class="mao">：</view>
                                <view class="content">
                                    <text v-if="item.NEWS_TYPE == 0">本地文章</text>
                                    <text v-if="item.NEWS_TYPE == 1">外部文章</text>
                                </view>
                            </view>

                            <view class="info-item">
                                <view class="title">分类</view>
                                <view class="mao">：</view>
                                <view class="content">【{{ item.NEWS_CATE_NAME }}】</view>
                            </view>

                            <view class="info-item">
                                <view class="title">排序号</view>
                                <view class="mao">：</view>
                                <view class="content">
                                    {{ item.NEWS_ORDER }}
                                    <text class="margin-left-xxs text-grey">(小的先显示)</text>
                                </view>
                            </view>

                            <view class="info-item">
                                <view class="title">创建</view>
                                <view class="mao">：</view>
                                <view class="content">{{ item.NEWS_ADD_TIME }}</view>
                            </view>

                            <view class="oprt">
                                <view @tap="url" :data-url="'../edit/admin_news_edit?id=' + item._id" class="btn round margin-right-s">编辑内容</view>

                                <view @tap="bindSortTap" v-if="item.NEWS_HOME > 0" :data-id="item._id" data-sort="0" class="btn margin-right-s">上首页</view>

                                <view @tap="bindSortTap" v-if="item.NEWS_HOME == 0" :data-id="item._id" data-sort="9999" class="btn margin-right-s">取消上首页</view>

                                <view @tap="bindStatusSelectTap" :data-id="item._id" class="btn margin-right-s">状态修改</view>

                                <view @tap="bindReviewTap" :data-id="item._id" class="btn margin-right-s">
                                    <text class="icon-attention margin-right-xxs"></text>
                                    预览
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
                <!-- List END -->

                <!-- load begin -->

                <!-- parse <template is="listLoadTpl" :data="dataList,skin:'text-grey'"/> -->
                <block name="listLoadTpl" v-if="false">
                    <view v-if="!dataList || (dataList && dataList.page < dataList.count)" :class="'load text-grey loading ' + 'text-grey'"></view>
                    <view v-if="dataList && dataList.page > 1 && dataList.page == dataList.count" :class="'load text-grey over ' + 'text-grey'"></view>

                    <block v-if="noHintImg">
                        <image style="height: 500rpx" mode="aspectFit" v-if="dataList && dataList.total == 0" :src="noHintImg" />
                    </block>

                    <block v-else>
                        <view v-if="dataList && dataList.total == 0" :class="'load text-grey ' + 'text-grey'">{{ '暂无记录' ? '暂无记录' : '暂无记录哦~' }}</view>
                    </block>

                    <view style="height: 100rpx"></view>
                </block>
                <!-- load end -->
            </cmpt-comm-list>
        </block>
    </view>
</template>

<script>
const AdminNewsBiz = require('../../../../biz/admin_news_biz.js');
const AdminBiz = require('../../../../biz/admin_biz.js');
const pageHelper = require('../../../../helper/page_helper.js');
const cloudHelper = require('../../../../helper/cloud_helper.js');
export default {
    data() {
        return {
            dataList: {
                total: '',
                list: [],
                page: 0,
                count: 0
            },

            sortItems: '',
            sortMenus: '',
            isAdmin: false,
            search: '',
            noHintImg: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: async function (options) {
        if (!AdminBiz.isAdmin(this)) {
            return;
        }

        //设置搜索菜单
        await this.getSearchMenuFun();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: async function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    methods: {
        url: async function (e) {
            pageHelper.url(e, this);
        },

        bindCommListCmpt: function (e) {
            pageHelper.commListListener(this, e);
        },

        bindSortTap: async function (e) {
            if (!AdminBiz.isAdmin(this)) {
                return;
            }
            let id = e.currentTarget.dataset.id;
            let sort = e.currentTarget.dataset.sort;
            if (!id || !sort) {
                return;
            }
            let params = {
                id,
                sort
            };
            try {
                await cloudHelper.callCloudSumbit('admin/news_sort', params).then((res) => {
                    pageHelper.modifyListNode(id, this.dataList.list, 'NEWS_HOME', sort);
                    this.setData({
                        dataList: this.dataList
                    });
                });
            } catch (e) {
                console.log('CatchClause', e);
                console.log('CatchClause', e);
                console.log(e);
            }
        },

        delFun: async function (id, that) {
            if (!AdminBiz.isAdmin(this)) {
                return;
            }
            if (!id) {
                return;
            }
            let params = {
                id
            };
            let callback = async () => {
                try {
                    let opts = {
                        title: '删除中'
                    };
                    await cloudHelper.callCloudSumbit('admin/news_del', params, opts).then((res) => {
                        pageHelper.delListNode(id, that.data.dataList.list, '_id');
                        that.data.dataList.total--;
                        that.setData({
                            dataList: that.data.dataList
                        });
                        pageHelper.showSuccToast('删除成功');
                    });
                } catch (e) {
                    console.log('CatchClause', e);
                    console.log('CatchClause', e);
                    console.log(e);
                }
            };
            pageHelper.showConfirm('确认删除？删除不可恢复', callback);
        },

        bindReviewTap: function (e) {
            let id = pageHelper.dataset(e, 'id');
            uni.navigateTo({
                url: pageHelper.fmtURLByPID('/pages/news/detail/news_detail?id=' + id)
            });
        },

        bindStatusSelectTap: async function (e) {
            if (!AdminBiz.isAdmin(this)) {
                return;
            }
            let itemList = ['启用', '停用', '删除'];
            let id = pageHelper.dataset(e, 'id');
            uni.showActionSheet({
                itemList,
                success: async (res) => {
                    switch (res.tapIndex) {
                        case 0: {
                            //启用
                            await this.setStatusFun(id, 1, this);
                            break;
                        }
                        case 1: {
                            //停止
                            await this.setStatusFun(id, 0, this);
                            break;
                        }
                        case 2: {
                            //删除
                            await this.delFun(id, this);
                            break;
                        }
                    }
                },
                fail: function (res) {}
            });
        },

        setStatusFun: async function (id, status, that) {
            status = Number(status);
            let params = {
                id,
                status
            };
            try {
                await cloudHelper.callCloudSumbit('admin/news_status', params).then((res) => {
                    pageHelper.modifyListNode(id, that.data.dataList.list, 'NEWS_STATUS', status, '_id');
                    that.setData({
                        dataList: that.data.dataList
                    });
                    pageHelper.showSuccToast('设置成功');
                });
            } catch (e) {
                console.log('CatchClause', e);
                console.log('CatchClause', e);
                console.log(e);
            }
        },

        getSearchMenuFun: async function () {
            let arr = await AdminNewsBiz.getCateList();
            let sortItems = [];
            let sortMenus = [
                {
                    label: '全部',
                    type: '',
                    value: ''
                },
                {
                    label: '正常',
                    type: 'status',
                    value: 1
                },
                {
                    label: '停用',
                    type: 'status',
                    value: 0
                },
                {
                    label: '首页推荐',
                    type: 'home',
                    value: 0
                }
            ];
            sortMenus = sortMenus.concat(arr);
            this.setData({
                sortItems,
                sortMenus
            });
        }
    }
};
</script>
<style>
@import './admin_news_list.css';
@import 'undefined';
</style>
